<?php
//更新用户资料
$userName = $_POST['username'];
$pw = $_POST['pw'];
$cpw = $_POST['cpw'];
$sex = $_POST['sex'];
$email = $_POST['email'];
$intro = $_POST['intro'];
$fav = implode(",",$_POST['fav']);
if($pw != $cpw){
    echo "<script>alert('如果需要修改密码，两次输入的密码必须相同');</script>";
    exit;
}
include_once "conn.php";
if($pw){  //说明要改密码
    $sql = "update userinfo set pw = '" . md5($pw) . "', email = '$email', fav = '$fav', intro = '$intro', sex = '$sex' where username = '$userName'";
}
else{  //说明不需要改密码
    $sql = "update userinfo set email = '$email', fav = '$fav', intro = '$intro', sex = '$sex' where username = '$userName'";
}
$result = mysqli_query($conn,$sql);
if($result){
    //说明更新成功
    echo "<script>alert('更新资料成功');location.href='index.php';</script>";
}
else{
    //说明更新失败
    echo "<script>alert('更新资料失败');history.back();</script>";
}